import os
import shutil
def getResults(filename):
    infile = open(filename)
    i = 0
    results = []
    for line in infile:
        line = line.strip()
        if i < 2:
            line = line.split(": ")

        else:
            line = line.split("is ")
            line[1] = line[1].strip(" km")

        results.append(line)
        i +=1

    return results

def fileExists(filename):
    if os.path.isfile(filename):
        return True
    else:
        print("File",filename, "does not exist")
        return False
def isCorrect():
    if not fileExists("walkable.txt"):
        return False
    if not fileExists("expected_walkable.txt"):
        return False
    results = getResults("walkable.txt")
    expected = getResults("expected_walkable.txt")
    if len(results) != len(expected):
        print("The two files have different number of lines")
        return False
    num = 0
    for i in range(len(results)):
        match = True
        if num > 0:
            if abs(float(results[i][1]) - float(expected[i][1])) > 0.000000000001:
                match = False
        else:
            if results[i] != expected[i]:
                match = False
        if not match:
            print("The records do no match. The first record that does not match is given below.")
            print("Your output: \n", results[i][0] + " " + results[i][1])
            print("Correct output: \n", expected[i][0] + " " + expected[i][1])
            return False
        num += 1
    return True
    #for line in results:



def deleteFile(filename):
    if os.path.isfile(filename):
        os.remove(filename)


def checkTestCase(testcase):
    print("Deleting files")
    deleteFile("walkable.txt")




    print("CHECKING TESTCASE: ", testcase)
    print("Running your code")
    # command = "echo " + testcase + " | task2.py"
    command = "echo " + testcase + " | python task2.py"
    print(command)
    os.system(command)




    print("checking results...")
    if isCorrect():
        print("The result is correct")
    else:
        print("The result is not correct")



checkTestCase("42.3:-124.2:43.3:-123.3")
